<template>
    <el-dialog :title="title" :visible.sync="open" width="40%" :before-close="handleClose" class="dialogClass">
        <!-- 表单开始 -->
        <el-form ref="ruleForm" :model="ruleForm" label-width="80px">
            <el-row :gutter="20">
                <el-col>
                    <el-form-item label="饲料类别" prop="siliaoleibie">
                        <el-input  v-model="ruleForm.siliaoleibie" placeholder="请输入饲料类别" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col>
                    <el-form-item label="拼音简码" prop="binyinjianma">
                        <el-input  v-model="ruleForm.binyinjianma" placeholder="请输入拼音简码" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col>
                    <el-form-item label="饲料名称" prop="siliangmingcheng">
                        <el-input  v-model="ruleForm.siliangmingcheng" placeholder="请输入饲料名称" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="20">
                <el-col>
                    <el-form-item label="饲料编号" prop="siliangbianhao">
                        <el-input  v-model="ruleForm.siliangbianhao" placeholder="请输入订单编号" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col>
                    <el-form-item label="饲料分类" prop="siliaofenlei">
                        <el-input  v-model="ruleForm.siliaofenlei" placeholder="请输入饲料分类" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <!-- 表单结束 -->
        <span slot="footer" class="dialog-footer">
            <el-button type="primary" size="big" @click="onSubmit('ruleForm')" :loading="loading">{{ submitButton
            }}</el-button>
            <el-button size="big" @click="resetForm('ruleForm')">重置</el-button>
            <el-button size="big" @click="close('ruleForm')">取消</el-button>
        </span>
    </el-dialog>
</template>

<script>
import { updateAPI, saveAPI } from "@/apis/stock/goodsManagement.js";
export default {
    data() {
    return {
      // 提交按钮
      submitButton: "",
      // 控制弹窗
      open: false,
      // 控制按钮加载状态
      loading: false,
      // 弹窗标题
      title: "",
      // 表单项
      ruleForm: {
        siliangbianhao: "",
        siliangmingcheng: "",
        siliaoleibie: "",
        binyinjianma: "",
        siliaofenlei: "",
      },
    };
  },
  methods: {
    //*查找货物类别
    queryCategory() {
      queryAllCategory()
        .then((res) => {
          this.options = res.data;
        })
        .catch(() => {});
    },
    //* 查找单位
    searchUnit() {
      const _this = this;
      queryAllUnit().then(function (res) {
        _this.unit = res.data;
      });
    },
    //* 控制弹窗
    show(title, row) {
      const _this = this;
      _this.title = title;
      _this.open = true;
      if (row) {
        _this.submitButton = "修改";
        _this.ruleForm = JSON.parse(JSON.stringify(row));
      } else {
        _this.submitButton = "确定";
      }
    },
    close(formName) {
      this.open = false;
      this.resetForm(formName);
      this.tableData = [];
    },
    //*关闭弹窗
    handleClose(done) {
      const _this = this;
      _this
        .$confirm("系统可能不会保存您的更改", "确认关闭？")
        .then((_) => {
          done();
          _this.resetForm("ruleForm");
        })
        .catch((_) => {});
    },
    //*修改
    onSubmit(formName) {
      let _this =this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (_this.title == "新增饲料") {
            saveAPI(_this.ruleForm).then(function (res) {
              if (res.code === 0) {
                _this.$alert(_this.ruleForm.siliangmingcheng + res.msg, "提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {
                    _this.close("ruleForm");
                    _this.$parent.$parent.search();
                  },
                });
              }
            });
          } else {
            updateAPI(_this.ruleForm).then(function (res) {
              if (res.code === 0) {
                _this.$alert(_this.ruleForm.siliangmingcheng + res.msg, "提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {
                    _this.close("ruleForm");
                    _this.$parent.$parent.search();
                  },
                });
              }
            });
          }
        }
      });
    },
    //* 表单重置
    resetForm(formName) {
      const _this = this;
      _this.$refs[formName].resetFields();
      _this.ruleForm.siliangmingcheng = ''
      _this.ruleForm.siliaoleibie=''
      _this.ruleForm.siliangbianhao = ''
      _this.ruleForm.siliaofenlei=''
      _this.ruleForm.binyinjianma=''      
    },
  },
};
</script>

<style></style>